/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

OutputWindow - 类名称
修改许可权限 - 修改人名称

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function OutputWindow()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.ui{
	import com.OpenFile;
	import com.bit101.components.InputText;
	import com.bit101.components.Label;
	import com.bit101.components.PushButton;
	import com.bit101.components.TextArea;
	import com.bit101.components.Window;
	
	import editor.common.Common;
	import editor.manager.MapManger;
	import editor.map.dataOutput.MapdataOutputer;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.controls.Alert;
	
	public class OutputWindow extends Window{
		
		private var _mapIdLabel:Label;
		private var _mapId:InputText;

		private var _mapNameLabel:Label;
		private var _mapName:InputText;
		
		private var _outputBtn:PushButton;
		private var _inputBtn:PushButton;
		
		// 构造函数
		public function OutputWindow(parent:DisplayObjectContainer=null, xpos:Number = 0, ypos:Number = 0)
		{
			super(parent, xpos, ypos, "文件");
			setSize(120,150);
			
			_mapIdLabel = new Label(this,10, 10, "地图id");
			_mapId = new InputText(this, 50, 10);
			_mapId.setSize(55,15);
			
			_mapNameLabel = new Label(this,10, 30, "地图名");
			_mapName = new InputText(this, 50, 30);
			_mapName.setSize(55,15);
			
			_outputBtn = new PushButton(this,10,50,"保存");
			_outputBtn.addEventListener(MouseEvent.CLICK, onOutput);
			
			_inputBtn = new PushButton(this,10, 75, "导入", onInput);
		}
		
		private function onOutput(evt:MouseEvent):void
		{
			if(_mapId.text == "" || _mapName.text == "")
				mx.controls.Alert.show("请输入地图名和地图id");
			else
			{
				Common.map.gmap.mapId = _mapId.text;
				Common.map.gmap.mapName = _mapName.text;
				MapManger.getInstance().tileLayer.outputTileData();
			}
			
//			MapdataOutputer.outPutXmlFile();
		}
		
		private function onInput(evt:MouseEvent):void
		{
			OpenFile.browseForOpen(onTileFind);
		}
		
		private function onTileFind(file:File):void
		{
			if(file != null)
			{
				var loader:Loader = new Loader();
				loader.loadBytes(OpenFile.open(file));
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onTileLoaded);
			}
		}
		
		private function onTileLoaded(evt:Event):void
		{
			var loader:Loader = evt.target.loader;
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onTileLoaded);
			var bmp:BitmapData = Bitmap(loader.content).bitmapData;
			MapManger.getInstance().tileLayer.inputTileData(MapdataOutputer.readTileBitmapdata(bmp));
		}
		//
	}
}